package com.sr.recitewords.controller;

import com.sr.recitewords.common.vo.GeneralResult;
import com.sr.recitewords.datasource.entities.SysUser;
import com.sr.recitewords.service.redis.RedisService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping(value = "/redisTest")
@Api(tags = {"redis测试接口"})
public class RedisTestController extends BaseController {
    @Resource
    private RedisService redisService;


    @ApiOperation(value = "设置String类型数据到redis中")
    @RequestMapping(value = "/setString", method = RequestMethod.POST)
    public GeneralResult<String> setString(@RequestBody String vale) {
        boolean b = redisService.setString("test00005", vale);
        return GeneralResult.genSuccessResult(b ? "设置成功" : "设置失败");
    }

    @ApiOperation(value = "获取String类型的值")
    @RequestMapping(value = "/getString", method = RequestMethod.GET)
    public GeneralResult<String> getString(@RequestBody String key) {
        String string = redisService.getString(key);
        return GeneralResult.genSuccessResult(string);
    }

    @ApiOperation(value = "置List类型数据到redis中")
    @RequestMapping(value = "/setList", method = RequestMethod.POST)
    public GeneralResult<String> setList(@RequestBody List<SysUser> sysUserList) {
        boolean b = redisService.setList("setList00005", sysUserList);
        return GeneralResult.genSuccessResult(b ? "设置成功" : "设置失败");
    }

    @ApiOperation(value = "获取List类型的值")
    @RequestMapping(value = "/getList", method = RequestMethod.GET)
    public GeneralResult<List<SysUser>> getList(@RequestBody String key) {
        List<SysUser> list = (List<SysUser>) redisService.getList(key, SysUser.class, 0, -1);
        return GeneralResult.genSuccessResult(list);
    }


}
